package com.software.loginregister.fragment.menudraftsfragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.software.loginregister.R;

import com.software.loginregister.activity.PublishWorkActivity;
import com.software.loginregister.adapter.WorksDraftAdapter;
import com.software.loginregister.api.WorksApi;
import com.software.loginregister.entity.Result;
import com.software.loginregister.entity.Works;
import com.software.loginregister.util.RetrofitClient;
import com.software.loginregister.util.SharedPreferencesUtil;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class WorksDraftFragment extends Fragment {
    private RecyclerView recyclerView;
    private WorksDraftAdapter adapter;
    private List<Works> draftList = new ArrayList<>();
    private WorksApi worksApi;
    private SwipeRefreshLayout swipeRefreshLayout;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_work_drafts, container, false);
        initViews(view);
        initData();
        return view;
    }

    private void initViews(View view) {
        recyclerView = view.findViewById(R.id.rv_works_drafts);
        swipeRefreshLayout = view.findViewById(R.id.srl_works_drafts);

        // 设置RecyclerView
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new WorksDraftAdapter(draftList);  // 使用正确的适配器类
        recyclerView.setAdapter(adapter);

        // 设置下拉刷新
        swipeRefreshLayout.setOnRefreshListener(this::loadDrafts);

        // 设置点击事件
        adapter.setOnItemClickListener(new WorksDraftAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Works draft, int position) {
                Intent intent = new Intent(getContext(), PublishWorkActivity.class);
                intent.putExtra("draftId", draft.getWorksId());
                intent.putExtra("isDraft", true);
                startActivity(intent);
            }
        });
    }

    private void initData() {
        worksApi = RetrofitClient.getInstance().create(WorksApi.class);
        loadDrafts();
    }

    private void loadDrafts() {
        // 获取实际的用户ID
        Long userId = SharedPreferencesUtil.getUserId(requireContext());
        if (userId == null) {
            Toast.makeText(getContext(), "用户未登录", Toast.LENGTH_SHORT).show();
            swipeRefreshLayout.setRefreshing(false);
            return;
        }

        Call<Result<List<Works>>> call = worksApi.getDrafts(userId.intValue());
        call.enqueue(new Callback<Result<List<Works>>>() {
            @Override
            public void onResponse(Call<Result<List<Works>>> call, Response<Result<List<Works>>> response) {
                swipeRefreshLayout.setRefreshing(false);
                if (response.isSuccessful() && response.body() != null && response.body().getCode() == 1) {
                    draftList.clear();
                    draftList.addAll(response.body().getData());
                    adapter.notifyDataSetChanged();
                } else {
                    Toast.makeText(getContext(), "获取草稿失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<Result<List<Works>>> call, Throwable t) {
                swipeRefreshLayout.setRefreshing(false);
                Toast.makeText(getContext(), "网络错误", Toast.LENGTH_SHORT).show();
            }
        });
    }
}